可刷新列表

1import { List, Navigation, NavigationStack, Script, Section, Text, useState } from "scripting"
2
3function Example() {
4  const [data, setData] = useState(generateRandomList)
5
6  function generateRandomList() {
7    const data: number[] = []
8    const count = Math.ceil(Math.random() * 100 + 10)
9
10    for (let i = 0; i < count; i++) {
11      const num = Math.ceil(Math.random() * 1000)
12      data.push(num)
13    }
14
15    return data
16  }
17
18  async function refresh() {
19    return new Promise<void>(resolve => {
20      setTimeout(() => {
21        setData(generateRandomList())
22        resolve()
23      }, 1000 * 2)
24    })
25  }
26
27  return <NavigationStack>
28    <List
29      navigationTitle={"Refreshable List"}
30      navigationBarTitleDisplayMode={"inline"}
31      refreshable={refresh}
32    >
33      <Section header={
34        <Text textCase={null}>Pull down to refresh</Text>
35      }>
36        {data.map(item =>
37          <Text>Number: {item}</Text>
38        )}
39      </Section>
40    </List>
41  </NavigationStack>
42}
43
44async function run() {
45  await Navigation.present({
46    element: <Example />
47  })
48
49  Script.exit()
50}
51
52run()